<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Insert title here</title>
<link href="/lib/jquery.mobile-1.3.2.css" rel="stylesheet" type="text/css">
<link href="/design.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/lib/jquery-2.0.3.js"></script>
<script type="text/javascript" src="/lib/jquery.mobile-1.3.2.js"></script>
<script type="text/javascript">

$(function(){
	var team_name = null;
	var team_id = null;
	//var team_id=79926;
	
	var now = new Date();
	var print_day = now.getDate() + 7; //이 날 까지만 자료를 뽑는다.
	var print_year = now.getYear();
	var print_month = now.getMonth();
	
	var url_year = '?game_year='+(print_year+1900);
	var url_month = '&game_month='+(print_month+1)+'&type=list';
	var url_team = null;
	//var url_team = '?team_id='+'79926';
	var url_total = null;
	
	$('#in').on('click', function(){
		team_name = $('#team_name').val();
		
		if( $('#team_name').val()){
			switch(team_name){
			case "샬럿 밥캐츠" :
			team_id = "79926"
			break;
		
			case "토론토 랩터스" :
			team_id = "79929"
			break;
			
			case "워싱턴 위저즈" :
			team_id = "79930"
			break;
			
			default:
			team_id = " "
			break;
			}
			console.log('team_id : ' + team_id);
		}
	
		url_team= '?team_id='+team_id;
		console.log(url_team);
		
		url_total = 'http://sports.media.daum.net/basketball/nba/record/tinf_schres.daum'+ url_team + url_year + url_month;
		console.log(url_total);
		
		$.ajax({
			url : '/new_schedule',
			//url : '/myTeam'
	        data : {url : url_total},
	        success : function(data){
		        //console.log('data : '+data);
		        //마이팀 정보
		        $('#myteam_name').text(team_name);
		        
		        var info = $(data).find('.info_box');
		        console.log('info : '+ info);
		        info.find('#info>table>tbody>tr th').each(function(idx,item){
		        	  
		        	 var team_year = $(item).find('tr td').text();
		        	 var team_local = $(item).find('tr td').text();
				     var team_win  = $(item).find('tr td').text();
				     var team_stadium = $(item).find('tr td').text();
				     var team_director = $(item).find('tr td').text();
				     var team_page = $(item).find('tr td').text();
				     
				     $('<td id="team_year">').text(text_year).appendTo($('.team_year'));
				     $('<td id="team_local">').text(text_local).appendTo($('.team_local'));
				     $('<td id="team_win">').text(text_win).appendTo($('.team_win'));
				     $('<td id="team_stadium">').text(text_stadium).appendTo($('.team_stadium'));
				     $('<td id="team_director">').text(text_director).appendTo($('.team_director'));
				     $('<td id="team_page">').text(text_page).appendTo($('.team_page'));
		        });
		        
		               
		       /*  
		        $('#team_year').text('td');
		        $('#team_local').text('');
		        $('#team_win').text('');
		        $('#team_stadium').text('');
		        $('#team_director').text('');
		        $('#team_page').text('');
		         */
		        
		        //마이팀 경기일정
	    	    var realrank =$(data).find('tbody');
	    	    console.log('tbody : '+ realrank);
		        $('#tbody_schedule').append('<tr>');
		        realrank.find('.today, .fast').each(function(idx,item){
		        	var num = 0;
	        	   	var text_date =$(item).find('.date').text();  
	         	  	var text_num = parseInt(text_date);//날짜에서 숫자만 따로 뺀 변수
	           	
	         	  	console.log('text_num :' + text_num); 
	        	   	console.log('text_date :' + text_date);
	           	
	         	  	if(text_num>print_day) return;
	           
	         	 	var text_team =$(item).find('.team1, .vs, .team2').text(); //과거게임
             	  	var text_team2 =$(item).find('.team').text(); //미래게임
               		var text_time =$(item).find('.time').text(); 
               		var text_place =$(item).find('.stadium').text();
               		var tr = $('<tr/>').appendTo($('#tbody_schedule'));
	
               		if(text_team && !text_team2){
               			$('<td class="date">').text(text_date).appendTo(tr);
	           			$('<td class="team"> ').text(text_team).appendTo(tr);
	           			$('<td class="time">').text(text_time).appendTo(tr);
		           		$('<td class="stadium">').text(text_place).appendTo(tr);
           			}else if(!text_team && !text_team2){//아예 경기가 없을 때
           				$('<td class="date">').text(text_date).appendTo(tr);
           				$('<td colspan="3" class="team" id="empty">').text('예정된 경기가 없습니다.').appendTo(tr);
           				$('<td class="time" >').text(" ").appendTo(tr);
	           			$('<td class="stadium" >').text(" ").appendTo(tr);
           			}else if(text_team2) {
	           			$('<td class="date">').text(text_date).appendTo(tr);
    	       			$('<td class="team">').text(text_team2).appendTo(tr);//미래게임
        	   			$('<td class="time">').text(text_time).appendTo(tr);
           				$('<td class="stadium">').text(text_place).appendTo(tr);
           			}
	           	console.log(text_date);
           });
         }//success
      });//ajax
	});//in onclick
});//function

</script>

</head>
<body>

<div data-role="page">
	<div data-role="header">
		<h3>SPO'CLOCK</h3>	
	</div>	
	<div data-role="content">
	<h1>MY TEAM</h1>
	<div data-role="collapsible-set" data-theme="a" data-content-theme="d">
		
		<div class="search">
		<input type="text" placeholder="팀 이름을 입력하세요" id="team_name"  value=""/>
		<button id="in">확인</button>
	</div>
	<div id="panel">
	
	<div class="info">
		<dl class="info_box_nba">
			<dt id="myteam_name"> </dt>
			<dd>
				<table>
					<colgroup>
						<col width="38">
						<col width="284">
						<col width="62">
						<col width="271">
					</colgroup>
					<tbody>
						<tr>
							<th class="team_year">창단</th ><th class="team_local">연고지</th>
						</tr>
						<tr>
							<th class="team_win">우승</th> <th class="team_stadium">홈구장</th>
						</tr>
						<tr>
							<th class="team_director">감독</th> <th class="team_page">홈페이지</th>
						</tr>
					</tbody>	
				</table>
			</dd>
			<dd id="info_2"><!-- 동부컨퍼런스 남동부 --></dd>
		</dl>
	</div>
	
	<!-- <button id="btn">load</button> -->
	<table class="schedule">
		<thead id="thead_schedule">
			<tr>
				<th class="date">날짜</th>
	 			<th class="team">팀</th>
				<th class="time">시간</th>
				<th class="stadium">구장</th>
			</tr>
		</thead>
			
		<tbody id="tbody_schedule">
			
		</tbody>
			
	</table>	
	</div>
		
		
			
		<div data-role="collapsible">
			<h3>NBA</h3>
			<fieldset data-role="controlgroup" data-iconpos="right">
				<input type="checkbox" name="checkbox-h-6a" id="checkbox-h-6a">
		        <label for="checkbox-h-6a">One</label>
		        <input type="checkbox" name="checkbox-h-6b" id="checkbox-h-6b">
		        <label for="checkbox-h-6b">Two</label>
		        <input type="checkbox" name="checkbox-h-6c" id="checkbox-h-6c">
		        <label for="checkbox-h-6c">Three</label>
			</fieldset>
		</div>
		<div data-role="collapsible">
			<h3>MLB</h3>
			<ul data-role="listview" id="MLB">
			
			</ul>
		</div>
		<div data-role="collapsible">
			<h3>UFC</h3>
			<ul data-role="listview" id="UFC">
			
			</ul>
		</div>
		<div data-role="collapsible">
			<h3>EPL</h3>
			<ul data-role="listview" id="EPL">
		
			</ul>
		</div>

	</div>	
	</div>
	
	
	
	<div data-role="footer" data-position="fixed">
		<div data-role="navbar">
			<ul>
				<li><a href="#main"> MAIN</a></li>
				<li><a href="#weekly">WEEKLY</a></li>
				<li><a href="#" class="ui-btn-active">MY TEAM</a></li>
				<li><a href="#gps">GPS</a></li>
			</ul>
		</div>
	</div>
</div>


</body>
</html>